package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/save")
    @ResponseBody
    public String save(){
        userService.saveAll();
        return "保存成功！";
    }
    @GetMapping("/findByName/{name}")
    public String findByName(@PathVariable String name, Model model){
        model.addAttribute("title","根据一个用户名查询用户");
        model.addAttribute("user",userService.findByName(name));
        return "All";
    }
    @GetMapping("/getOne/{id}")
    public String getOne(@PathVariable int id, Model model){
        model.addAttribute("title","根据id查询用户");
        model.addAttribute("user",userService.getOne(id));
        return "All";
    }
    @GetMapping("/findAll")
    public String findAll(Model model){
        model.addAttribute("title","查询所有用户");
        model.addAttribute("user",userService.findAll());
        return "All";
    }
    @GetMapping("/findByNameLike/{name}")
    public String findByNameLike(@PathVariable String name, Model model){
        model.addAttribute("title","根据一个用户名查询用户");
        model.addAttribute("user",userService.findByNameLike(name));
        return "All";
    }
}
